const initMap = (divId, options) => {
    const env = process.env;
    if (env.NODE_ENV === "development") {        
        //return initBaiduMap(divId, options);
        return initDevMap(divId, options);
    } else if (env.NODE_ENV === "production") {
        //return initProdMap(divId, options);
        return initBaiduMap(divId, options);
    }
};
/**
 * 开发环境下的地图初始化
 * @param divId 
 */
const initDevMap = (divId, options) => {
    console.log('开发环境下的地图初始化');
    var basemapLayer0 = L.tileLayer('http://t1.tianditu.com/vec_c/wmts?layer=vec&style=default&tilematrixset=c&Service=WMTS&Request=GetTile&Version=1.0.0&Format=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&tk=8899fd3e86aa994f71465b1c56a98727',
        {
            maxZoom: 18,
            minZoom: 1,
            tileSize: 256,
            zoomOffset: 1
        });
    var basemapLayer1 = L.tileLayer('http://t1.tianditu.com/cva_c/wmts?layer=cva&style=default&tilematrixset=c&Service=WMTS&Request=GetTile&Version=1.0.0&Format=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&tk=8899fd3e86aa994f71465b1c56a98727',
        {
            maxZoom: 18,
            minZoom: 1,
            tileSize: 256,
            zoomOffset: 1
        });
    var basemapLayer2 = L.tileLayer('http://t1.tianditu.com/img_c/wmts?layer=img&style=default&tilematrixset=c&Service=WMTS&Request=GetTile&Version=1.0.0&Format=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&tk=8899fd3e86aa994f71465b1c56a98727',
        {
            maxZoom: 18,
            minZoom: 1,
            tileSize: 256,
            zoomOffset: 1
        });
    var basemapLayer3 = L.tileLayer('http://t1.tianditu.com/cia_c/wmts?layer=cia&style=default&tilematrixset=c&Service=WMTS&Request=GetTile&Version=1.0.0&Format=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&tk=174705aebfe31b79b3587279e211cb9a',
        {
            maxZoom: 18,
            minZoom: 1,
            tileSize: 256,
            zoomOffset: 1
        });
    var basemap0 = L.layerGroup([basemapLayer0, basemapLayer1]);
    var basemap1 = L.layerGroup([basemapLayer2, basemapLayer3]);

    // 初始化地图，并设置地理中心和缩放倍数
    let mapOpt = {
        preferCanvas: true,
        crs: L.CRS.EPSG4326,
        layers: [basemap0],
        zoomControl: false,
        attributionControl: false,
        doubleClickZoom: false,
        editable: true//绘制控件
    };
    // let defCenter = [39.969258, 116.460323];
    let defCenter = [23.088981, 113.323540];
    let defZoom = 12;
    if(typeof(options)!="undefined") {
        defZoom = options.zoom || defZoom;
        if (options.center && options.center.lng && options.center.lat) {
            defCenter = [options.center.lat, options.center.lng];
        }
    }
    let map = L.map(divId, mapOpt).setView(defCenter, defZoom);
    return map;
};

const initProdMap = (divId) => {
    console.log('测试/部署环境下的地图初始化');
    var url = 'http://10.164.4.21:8084/api/map/wmts';
    let map = L.map(divId, {
        crs: L.CRS.NonEarthCRS({
            resolutions: [
                52435.4584709169,
                52435.4584709169 / 2,
                52435.4584709169 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
                52435.4584709169 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2,
            ],
            bounds: L.bounds([716637.9919559672, 546964.4106869339], [894455.6954487278, 728066.4921236223]),
            origin: L.point(-4823200, 6183000),
        }),
        center: [602112.0353794582, 805376.8412945375],
        maxZoom: 18,
        minZoom: 8,
        zoom: 14,
    });
    L.supermap
        .wmtsLayer(url, {
            layer: 'Shiliang', //Sate_2012  Shiliang_2018    DEM_2008
            style: 'Default',
            tilematrixSet: 'bj2000', //Sate_2012  Shiliang_2018
            format: 'image/png',
            tileSize: 512,
        })
        .addTo(this.map);
    // 朝阳区描面
    if (this.cyRegionGroup) {
        this.map.removeLayer(this.cyRegionGroup);
    }
    var cyRegionData = JSON.parse(JSON.stringify(cyRegion));
    floodMap.getMapJsonByName({ names: 'cyRegion' }).then(res => {
        if (res.code === '100' && res.data && res.data.cyRegion) {
            cyRegionData = JSON.parse(res.data.cyRegion);
        }
        this.cyRegionGroup = new L.FeatureGroup();
        const lineWeight = 3;
        const lineColor = '#0084ff';
        const lineOpacity = 1;
        L.geoJSON(cyRegionData, {
            style: function (feature) {
                return {
                    fillColor: '#0084ff',
                    color: lineColor,
                    opacity: lineOpacity,
                    fillOpacity: 0.16,
                    weight: lineWeight,
                };
            },
        }).addTo(this.cyRegionGroup);
        this.map.addLayer(this.cyRegionGroup);
        this.map.on('click', e => {
            console.log('click');
            var center = e.latlng;
            let latLng = center.lng + ',' + center.lat;
            // this.currentLatLng = e.latlng;
            this.getAddrBylnglat(latLng, center);
        });

        let t = this;
        this.map.on('zoomend', function () {
            t.zoom = t.map.getZoom();
            // console.log(t.currentLatLng);
            // let center = L.latLng(t.currentLatLng.lat, t.currentLatLng.lng);
            // console.log(center);
            // t.map.setView(center, zoom);
        });
    });

    return map;
};

const initBaiduMap = (divId) => {
    L.CRS.Baidu = new L.Proj.CRS(
        "EPSG:900913",
        "+proj=merc +a=6378206 +b=6356584.314245179 +lat_ts=0.0 +lon_0=0.0 +x_0=0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs",
        {
            resolutions: (function () {
                let level = 19;
                let re = 18;
                let res = [];
                res[0] = Math.pow(2, re);
                for (var i = 1; i < level; i++) {
                    res[i] = Math.pow(2, re - i);
                }
                return res;
            })(),
            origin: [0, 0],
            bounds: L.bounds([20037508.342789244, 0], [0, 20037508.342789244]),
        }
    );
    L.tileLayer.baidu = function (option) {
        option = option || {};
        var layer;
        var subdomains = "0123456789";
        switch (option.layer) {
            //单图层
            case "vec":
            default:
                //"http://online{s}.map.bdimg.com/tile/?qt=tile&x={x}&y={y}&z={z}&styles=pl&b=0&limit=60&scaler=1&udt=20170525"
                // layer = L.tileLayer("http://online{s}.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=" + (option.bigfont ? "ph" : "pl") + "&scaler=1&p=1", {
                //     name: option.name, subdomains: subdomains, tms: true
                // });
                layer = L.tileLayer(
                    "https://maponline0.bdimg.com/tile/?qt=vtile&x={x}&y={y}&z={z}&styles=pl&scaler=2&udt=20230105&from=jsapi2_0",
                    {
                        name: option.name,
                        subdomains: subdomains,
                        tms: true,
                    }
                );
                break;
            case "img_d":
                layer = L.tileLayer(
                    "http://shangetu{s}.map.bdimg.com/it/u=x={x};y={y};z={z};v=009;type=sate&fm=46",
                    {
                        name: option.name,
                        subdomains: subdomains,
                        tms: true,
                    }
                );
                break;
            case "img_z":
                layer = L.tileLayer(
                    "http://online{s}.map.bdimg.com/tile/?qt=tile&x={x}&y={y}&z={z}&styles=" +
                    (option.bigfont ? "sh" : "sl") +
                    "&v=020",
                    {
                        name: option.name,
                        subdomains: subdomains,
                        tms: true,
                    }
                );
                break;

            case "custom": //Custom 各种自定义样式
                //可选值：dark,midnight,grayscale,hardedge,light,redalert,googlelite,grassgreen,pink,darkgreen,bluish
                option.customid = option.customid || "dark";
                layer = L.tileLayer(
                    //   "http://api{s}.map.bdimg.com/customimage/tile?&x={x}&y={y}&z={z}&scale=1&customid=" + option.customid,
                    "http://api{s}.map.bdimg.com/customimage/tile?&x={x}&y={y}&z={z}&ak=yipxfCwhMDozKT3GDCM7UGARmSKbAMXh&scale=1&customid=" +
                    option.customid,
                    {
                        name: option.name,
                        subdomains: "012",
                        tms: true,
                    }
                );
                break;

            case "time": //实时路况
                var time = new Date().getTime();
                layer = L.tileLayer(
                    "http://its.map.baidu.com:8002/traffic/TrafficTileService?x={x}&y={y}&level={z}&time=" +
                    time +
                    "&label=web2D&v=017",
                    {
                        name: option.name,
                        subdomains: subdomains,
                        tms: true,
                    }
                );
                break;

            //合并
            case "img":
                layer = L.layerGroup([
                    L.tileLayer.baidu({
                        name: "底图",
                        layer: "img_d",
                        bigfont: option.bigfont,
                    }),
                    L.tileLayer.baidu({
                        name: "注记",
                        layer: "img_z",
                        bigfont: option.bigfont,
                    }),
                ]);
                break;
        }
        return layer;
    };

    let vec = new L.tileLayer.baidu({ layer: "vec", name: "矢量图" });

    var options = {
        center: [39.981775, 116.524372], // The initial center(baidu BD-09 format) of map
        zoom: 14, // initial zoom of map
        crs: L.CRS.Baidu,
        layers: [vec],
    };
    let map = L.map(divId, options);
    
    const baseLayers = {
        矢量图: vec
    };    

    // 添加图层控制控件
    //L.control.layers(baseLayers).addTo(map);    

    map.on("click", (e) => {
        console.log("[百度地图]鼠标点击位置坐标：" + e.latlng.lng + " , " + e.latlng.lat);
    });
    
    return map;
}

const testOut = () => {
    console.log('测试环境');
};

export default {
    initMap,
    testOut
}